package gui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

import main.Controller;

/**
 * 
 * @author Christian
 * TransferHandler für die Listen
 */
public class ListTransferHandler extends TransferHandler {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private JList list = null;
	
	/**
	 * 
	 */
	private String zauberName = null;
	
	/**
	 * 
	 */
	private String repraesentation = null;
	
	/**
	 * 
	 * @param list Liste mit Varianten
	 * @param zauberName Zaubername
	 * @param repraesentation Repräsentation
	 */
	public ListTransferHandler(JList list, String zauberName, String repraesentation) {

		this.list = list;
		this.zauberName = zauberName;
		this.repraesentation = repraesentation;
	}
	
	/**
	 * @param info Test ob die Daten importiert werden kann.
	 * @return true wenn das Importieren möglich ist.
	 */
	public boolean canImport(TransferHandler.TransferSupport info) {

		// we only import Strings
		if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			return false;
		}
		JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
		if (dl.getIndex() == -1) {
			return false;
		}
		return true;
	}
	
	/**
	 * @param info Importiert die Daten in die Liste
	 * @return true wenn das Importieren geklappt hat
	 */
	public boolean importData(TransferHandler.TransferSupport info) {

		if (!info.isDrop()) {
			return false;
		}
		// Check for String flavor
		if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			return false;
		}
		JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
		DefaultListModel listModel = (DefaultListModel) list.getModel();
		int index = dl.getIndex();
		boolean insert = dl.isInsert();
		// Get the string that is being dropped.
		Transferable t = info.getTransferable();
		String data;
		try {
			data = (String) t.getTransferData(DataFlavor.stringFlavor);
		} catch (Exception e) {
			return false;
		}
		// Perform the actual import.
		int indexOfData = listModel.indexOf(data);
		listModel.removeElement(data);
		if (indexOfData != -1 && index >= indexOfData) {
			index--;
		}
		if (insert) {
			listModel.add(index, data);
		} else {
			listModel.set(index, data);
		}
		String[] strings = new String[list.getModel().getSize()];
		for (int i = 0; i < strings.length; i++) {
			strings[i] = (String) list.getModel().getElementAt(i);
			// System.out.println(strings[i]);
		}
		if (zauberName.equals("Alle")) {
			Controller.getDatenController().setDaten(strings, "Alle", "Alle");
		} else {
			Controller.getDatenController().setDaten(strings, zauberName, repraesentation);
		}
		return true;
	}
	
	/**
	 * @param c JComponent
	 * @return Copy
	 */
	public int getSourceActions(JComponent c) {

		return COPY;
	}
	
	/**
	 * @param c JComponent
	 * @return Transferobject
	 */
	protected Transferable createTransferable(JComponent c) {

		JList transList = (JList) c;
		Object[] values = transList.getSelectedValues();
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < values.length; i++) {
			Object val = values[i];
			String val2 = "";
			if (val != null) {
				val2 = val.toString();
			}
			buff.append(val2);
			if (i != values.length - 1) {
				buff.append("\n");
			}
		}
		return new StringSelection(buff.toString());
	}
}
